package libs.yzx.zaza.acts;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2016/8/23
 */
public class CustomerViewGroup extends ViewGroup{

    public CustomerViewGroup(Context context) {
        super(context);
    }

    public CustomerViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomerViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        /*先测量出全部子View的宽高(measureWidth和measureHeight)*/
        measureChildren(widthMeasureSpec,heightMeasureSpec);

        /* 判断自己的layout_width的设置情况 */
        int widthResult = 0,heightResult = 0;
        if(MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY){
            widthResult = MeasureSpec.getSize(widthMeasureSpec);
        }else{
            for (int i = 0; i < getChildCount(); i++)
                widthResult += getChildAt(i).getMeasuredWidth();
        }

        /* 判断自己的layout_height的设置情况 */
        if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY){
            heightResult = MeasureSpec.getSize(heightMeasureSpec);
        }else{
            for (int i = 0; i < getChildCount(); i++)
                heightResult += getChildAt(i).getMeasuredHeight();
        }

        setMeasuredDimension(widthResult , heightResult);
    }


    @Override
    protected void onLayout(boolean change, int l, int t, int r, int b) {
        View child = getChildAt(0);
        child.layout( 0 , 0 , child.getMeasuredWidth(), child.getMeasuredHeight());

        View child2 = getChildAt(1);
        child2.layout(child.getMeasuredWidth(),
                                    child.getMeasuredHeight(),
                                    child.getMeasuredWidth()+child2.getMeasuredWidth(),
                                    child.getMeasuredHeight()+child2.getMeasuredHeight());

    }

}
